//
//  WPDetailSearchView.m
//  wobingwoyi
//
//  Created by WGP on 16/7/25.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "WPDetailSearchView.h"

@interface WPDetailSearchView ()<UITextFieldDelegate>

@property(nonatomic,strong)UITextField *textField;
@property(nonatomic,copy)cancelDidClickBlock cancelBlock;

@end

@implementation WPDetailSearchView

-(instancetype)initWith:(cancelDidClickBlock)cancelBlock
{
    if (self = [super initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-2*15, 30)]) {
        self.cancelBlock = cancelBlock;
        
        self.backgroundColor = [UIColor clearColor];
        [self setupUI];
    }
    return self;
}

-(void)setupUI
{
    [self cancelBtn];
    [self bgView];
    [self iconView];
    
//    1.创建子控件
    self.textField = [[UITextField alloc] init];
    self.textField.backgroundColor = [UIColor whiteColor];
    self.textField.placeholder = @"输入疾病、医院、医生";
    self.textField.font = [UIFont systemFontOfSize:14];
    self.textField.textColor = GLobalFontColor;
    self.textField.returnKeyType = UIReturnKeySearch;
    self.textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.textField.delegate = self;
    [self.bgView addSubview:self.textField];
    
//    2.添加约束
    
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconView.mas_right).offset(10);
        make.top.bottom.right.equalTo(self.bgView);
        
    }];
}

-(UIButton *)cancelBtn
{
    if (_cancelBtn == nil)
    {
        _cancelBtn = [[UIButton alloc] init];
        [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _cancelBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_cancelBtn addTarget:self action:@selector(cancelBtnDidClick) forControlEvents:UIControlEventTouchDown];
        [self addSubview:_cancelBtn];
        
        CGSize size = [_cancelBtn.titleLabel.text sizeWithMaxSize:CGSizeMake(MAXFLOAT, MAXFLOAT) font:14];
        [_cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self);
            make.width.mas_equalTo(size.width+1);
            make.centerY.equalTo(self);
        }];
    }
    return _cancelBtn;
}

-(UIView *)bgView
{
    if (_bgView == nil)
    {
        _bgView = [UIView new];
        _bgView.backgroundColor = [UIColor whiteColor];
        _bgView.layer.cornerRadius = 4;
        _bgView.layer.masksToBounds = YES;
        [self addSubview:_bgView];
        
        [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.bottom.equalTo(self);
            make.right.equalTo(self.cancelBtn.mas_left).offset(-12.5);
        }];
    }
    return _bgView;
}

-(UIImageView *)iconView
{
    if (_iconView == nil)
    {
        _iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"main_search"]];
        [_iconView sizeToFit];
        [self addSubview:_iconView];
        
        [_iconView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(10);
            make.height.width.mas_equalTo(15);
            make.centerY.equalTo(self);
        }];
    }
    return _iconView;
}

-(void)cancelBtnDidClick
{
    self.cancelBlock();
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

@end










